package com.fingersoft.im.crash

import com.fingersoft.business.BusinessContext
import com.fingersoft.im.api.base.BaseModelCallback2
import com.fingersoft.im.utils.FileUtils
import okhttp3.Call
import okhttp3.Response
import java.io.File

/**
 * @author：jooper  Email：jooperge@163.com
 * 描述：
 * 修改历史:
 * <p>
 * 创建于： 2021/4/21
 */
class CrashHelper {
    suspend fun upload() {
        val crashRoot = File(CrashHandler.getInstance().crashBakPath)
        crashRoot.takeIf { it.isDirectory }?.let {
            it.listFiles()?.let { files ->
                BusinessContext.api?.upload("am/errorLog/upload", HashMap(), files.asList(), object : BaseModelCallback2<CrashResponse>(CrashResponse::class.java) {
                    override fun onSuccess(modelResponse: CrashResponse?, call: Call?, response: Response?) {
                        super.onSuccess(modelResponse, call, response)
                        if (200 == modelResponse?.code) {
                            FileUtils.deleteDir(CrashHandler.getInstance().crashBakPath)
                        }
                    }
                })
            }
        }
    }
}
